pkgname=mongodb
#pkgver=r4.1.6
pkgver=r4.2.0
pkgrel=0
arch=('x86_64')
url="https://www.mongodb.com/"
pkgdesc="A high-performance, open source, schema-free document-oriented database"
license=('AGPL3')
depends=()
#makedepends_debian_11=(libcurl4-openssl-dev scons python3-pip python3-dev libssl-dev)
#source=("https://github.com/mongodb/mongo.git")
source=("https://gitee.com/ssri-mirrors/mongo.git")
sha256sums=('SKIP')

build() {
        if [ $os = "centos" ]
        then
                mv -f /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
                wget http://mirrors.aliyun.com/repo/Centos-altarch-7.repo -O /etc/yum.repos.d/CentOS-Base.repo
                yum install -y deltarpm
        fi
        
        PATH=$PATH:/root/.local/bin
        echo "os_arch = $os_arch   arch = $arch    PATH = $PATH"
        
	local PIP=pip

	which pip3 > /dev/null && PIP=$(which pip3)
	
        if [ $os = "openeuler" ]
        then
                $PIP install psutil
                $PIP install Cheetah3
        elif [ $os = "centos" ]
        then
                $PIP install wheel
        fi	

	cd "${srcdir}/mongo"
	git checkout -f r4.2.0

	# ImportError: 'module' object has no attribute 'check_specifier' on aliyun OS
        if [ $os = "centos" ]
        then
                $PIP install setuptools==58.2.0
        else
                $PIP install setuptools --upgrade
        fi

	for i in $(seq 1 10)
	do
		$PIP install -i https://mirrors.aliyun.com/pypi/simple --user -r etc/pip/dev-requirements.txt && break
	done


        if [ $os = "centos" ]
        then
                mirror_addr="https://mirrors.tuna.tsinghua.edu.cn/centos-altarch"
                sed -e 's|^mirrorlist=|#mirrorlist=|g' \
                        -e "s|^#baseurl=http://mirror.centos.org/centos|baseurl=${mirror_addr}|g" \
                        -i /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo
                yum repolist && yum clean && yum makecache
                yum install -y "devtoolset-9-gcc"
                yum install -y "devtoolset-9-gcc-c++"
                source /opt/rh/devtoolset-9/enable
                build_opts="CC=/opt/rh/devtoolset-9/root/usr/bin/gcc CXX=/opt/rh/devtoolset-9/root/usr/bin/g++"
        fi

        if [ "$os_arch" = "aarch64" ]
        then
                echo "going to arch = aarch64 branch"
                buildscripts/scons.py core CCFLAGS="-march=armv8-a+crc -mtune=generic" --disable-warnings-as-errors -j $(nproc) $build_opts
        else
                echo "going to else branch"
                buildscripts/scons.py core --disable-warnings-as-errors -j $(nproc) $build_opts
        fi
}

package() {
	mkdir -p "${pkgdir}/lkp/benchmarks/${pkgname}/bin"
	cd "${srcdir}/mongo"
	cp mongo* "${pkgdir}/lkp/benchmarks/${pkgname}/bin"
}
